package com.easybenefit.UUService.fragment;


import android.app.Fragment;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.amap.api.location.AMapLocationListener;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.easybenefit.UUService.R;


/**
 * 地图fragment
 */
public class MyMapFragment extends Fragment {

    private MapView mapView;
    private AMap aMap;

    public static MyMapFragment newInstance() {
        MyMapFragment fragment = new MyMapFragment();
        fragment.setRetainInstance(true);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_map, null);
        mapView = (MapView) contentView.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initData();
        return contentView;
    }

    public void initData() {
        double longitude = getArguments().getDouble("longitude");
        double latitude = getArguments().getDouble("latitude");
        String address = getArguments().getString("address");
        aMap = mapView.getMap();
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        //覆盖物的选项类
        MarkerOptions markerOptions = new MarkerOptions().
                position(new LatLng(latitude, longitude)).snippet(address).period(50).icon(BitmapDescriptorFactory.
                fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.poi_marker_pressed)));
//        Marker marker =
        aMap.addMarker(markerOptions);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15));
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     * map的生命周期方法
     */
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();

    }
}
